<?php
defined('_JEXEC') or die('Restricted access');
$app = JFactory::getApplication();
$input = $app->input;
$action = $input->get('action');
$getArray = $app->input->getArray();
$campaign = $getArray['campaign'];
$campaign_id = $input->get('campaign_id');
$app_id = $input->get('app_id');
?>

<?php if($action == 'create'): ?>
	<link href="/templates/ppspconsole/css/select2.min.css" type="text/css" rel="stylesheet" />
    <link href="/templates/ppspconsole/css/jquery.range.css" type="text/css" rel="stylesheet" />
	<?php
	$applist = $getArray['applist'];
	?>
<div class="res-title-rg">
    <span class="fl pos-icon text-c"><b class="glyphicon glyphicon-plus fontsize20"></b></span>
    <div class="title-info">
        <h2 class="fontsize20">New Campaign Channel</h2>
        <p><a href="#">Home</a>&nbsp;/&nbsp;Setup&nbsp;/&nbsp;New Campaign</p>
    </div>
</div>
	<form action="/index.php?option=com_console&Itemid=127&task=CampaignChannel&action=store" method="post" class="form-horizontal J_ajaxFsorm" >
		<input type="hidden" name="campaign_id" value="<?php echo $campaign_id;?>">
<div class="ws-content">
    	<div class="pdall15 bgwhite" style="width:820px; margin:0 auto;">
    		<div class="mobitems">
    			<span class="fl mobiname fontsize14">&nbsp;</span>
    			<div class="fl fontsize20">BASE</div>
    			<div class="clear"></div>
    		</div>

			<?php
			$channelist= $getArray['channelist'];
			?>
    		<div class="mobitems pos_pre">
    			<span class="fl mobiname fontsize14 lg36">Channel</span>
    			<div class="pos_pre">
                    <select class="select2" name="channel_id" id="channel_id">
						<?php if($channelist):?>
						<?php foreach($channelist as $val):?>
                      <option value="<?php echo $val->id?>"><?php echo $val->name?></option>
						<?php endforeach;;?>
						<?php endif;?>
                    </select>
                </div>
    			<div class="clear"></div>
    		</div>
			<div class="costitle tymgbt" style="padding-left:268px;">
				<span id="cost"></span>
				<strong>Cost from channel</strong>
				&nbsp;&nbsp;Cost&nbsp;&nbsp;<input type="text" value="0" class="text-c" name="cost" />
				<input type="hidden" name="cost_src" value="0" />
			</div>
            <div class="event-enable bgwhite fontsize14">
                <div class="eventile tymgbt"><span class="selected"></span><strong>Enable S2S Event Sync</strong><input type="hidden" name="enable_sync" value="1" /></div>
                <div class="enable-content">
                    <table class="table table-striped">
                      <thead>
                        <tr>
                          <th>Event</th>
                          <th>Partner Event Tag</th>
                          <th></th>
                        </tr>
                      </thead>
                      <tbody id="InputsWrapper">
                        
                      </tbody>
                    </table>
                    <div class="mobiadd">Search
                        <select class="select2" id="addsearch" style="width:230px;" >
                        </select> &nbsp;&nbsp;&nbsp; Link To&nbsp;&nbsp;&nbsp;MobiSoc Event Tag<input type="text" id="addeventag" />
    					<a href="javascript:void(0)" id="addmobisoc">+</a>
                    </div>
                </div>

				<div class="mobitems">
					<span class="fl mobiname fontsize14">Tracking URL</span>
					<div class="fl mobiurl" style="height: 100px;" id="tracking_url"></div>
					<div class="clear"></div>
				</div>
                <div class="text-r"><button type="submit" style="border: 0;" class="btn-popup colwhite fontsize16 J_ajax_submit_btn">Save & Close</button> <button type="submit" style="border: 0;" class="btn-popup colwhite fontsize16 J_ajax_submit_btn">Save & Close</button></div>

            </div>
    	</div>
</div>
	</form>
	<script type="text/javascript">
		$(function(){
			var appid = '<?php echo $app_id;?>';
			var appurl = '/index.php?option=com_console&view=campaigntracking&Itemid=127&task=CampaignTracking&action=ajax';
			getEvent(appid,appurl);

			var channel_id = $('#channel_id').val();
			getTracking(channel_id,'/index.php?option=com_console&Itemid=127&task=CampaignChannel&action=ajax');
			$('#channel_id').change(function () {
				var channel_id = $(this).val();
				getTracking(channel_id,'/index.php?option=com_console&Itemid=127&task=CampaignChannel&action=ajax');
			});
		});

		function getEvent(appid,url) {
			if (appid > 0) {
				$("#addsearch").empty();
				var url = url+'&appid='+appid;
				$.getJSON(url, function (data) {
					$.each(data,function(i,v){
						$("#addsearch").append("<option value='"+ v.event_id+"'>"+ v.event_name+"</option>");
					})
				});
			}
		}
		function getTracking(channel_id,url) {
			if (channel_id > 0) {

				var url = url+'&channel_id='+channel_id;
				$.getJSON(url, function (data) {
					var open_id = '<?php echo $campaign->open_id?>';
					var tracking_url = data.tracking_url;
					tracking_url = tracking_url.replace('{openId}',open_id);
					tracking_url = tracking_url.replace('{partnerId}',data.id);
					$('#tracking_url').html(tracking_url);
				});
			}
		}
	</script>
<?php elseif($action == 'edit'):?>
	<?php
	$id = $input->get('id');
	$model = $this->getModel();
	$item = $model->getItem($id);
	$maplist = $model->getCampaigEventMapping($id);
	?>
	<link href="/templates/ppspconsole/css/select2.min.css" type="text/css" rel="stylesheet" />
	<link href="/templates/ppspconsole/css/jquery.range.css" type="text/css" rel="stylesheet" />
	<?php
	$applist = $getArray['applist'];
	?>
	<div class="res-title-rg">
		<span class="fl pos-icon text-c"><b class="glyphicon glyphicon-plus fontsize20"></b></span>
		<div class="title-info">
			<h2 class="fontsize20">New Campaign Channel</h2>
			<p><a href="#">Home</a>&nbsp;/&nbsp;Setup&nbsp;/&nbsp;New Campaign</p>
		</div>
	</div>
	<form action="/index.php?option=com_console&Itemid=127&task=CampaignChannel&action=update" method="post" class="form-horizontal J_ajaxFsorm" >
		<input type="hidden" name="id" value="<?php echo $item->id;?>">
		<div class="ws-content">
			<div class="pdall15 bgwhite" style="width:820px; margin:0 auto;">
				<div class="mobitems">
					<span class="fl mobiname fontsize14">&nbsp;</span>
					<div class="fl fontsize20">BASE</div>
					<div class="clear"></div>
				</div>

				<?php
				$channelist= $getArray['channelist'];
				?>
				<div class="mobitems pos_pre">
					<span class="fl mobiname fontsize14 lg36">Channel</span>
					<div class="pos_pre">
						<select class="select2" name="channel_id" id="channel_id" disabled>
							<?php if($channelist):?>
								<?php foreach($channelist as $val):?>
									<option value="<?php echo $val->id?>" <?php if($item->channel_id == $val->id):?>selected<?php endif;?>><?php echo $val->name?></option>
								<?php endforeach;;?>
							<?php endif;?>
						</select>
					</div>
					<div class="clear"></div>
				</div>
				<div class="costitle tymgbt" style="padding-left:268px;">
					<span id="cost" <?php if($item->cost_src == 1):?>class="selected"<?php endif;?>></span>
					<strong>Cost from channel</strong>
					&nbsp;&nbsp;Cost&nbsp;&nbsp;
					<?php if($item->cost_src == 1):?>
					<input type="text" value="-1" class="text-c" name="cost" disabled  />
					<?php else :?>
					<input type="text" value="<?php echo $item->cost?>" class="text-c" name="cost" />
					<?php endif;?>
					<input type="hidden" name="cost_src" value="<?php echo $item->cost_src?>"  />
				</div>
				<div class="event-enable bgwhite fontsize14">
					<div class="eventile tymgbt"><span <?php if($item->enable_sync == 1):?>class="selected"<?php endif;?>></span><strong>Enable S2S Event Sync</strong><input type="hidden" name="enable_sync" value="<?php echo $item->enable_sync?>" /></div>
					<div class="enable-content"  <?php if($item->enable_sync == 0):?> style="display: none" <?php endif;?>>
						<table class="table table-striped">
							<thead>
							<tr>
								<th>Event</th>
								<th>Partner Event Tag</th>
								<th></th>
							</tr>
							</thead>
							<tbody id="InputsWrapper">
							<?php if($maplist):
							foreach($maplist as $k=>$v):
							?>
							<tr>
								<td><?php echo $v->event_name;?><input type="hidden" name="map[event_id][]" value="<?php echo $v->event_id?>"></td>
								<td><?php echo $v->partner_event_id?><input type="hidden" name="map[event_tag][]" value="<?php echo $v->partner_event_id?>"></td>
								<td><a href="javascript:void(0)" class="removetflist glyphicon glyphicon-remove colred"></a></td>
							</tr>
							<?php
							endforeach;
							endif;
							?>
							</tbody>
						</table>
						<div class="mobiadd">Search
							<select class="select2" id="addsearch" style="width:230px;" >
							</select> &nbsp;&nbsp;&nbsp; Link To&nbsp;&nbsp;&nbsp;MobiSoc Event Tag<input type="text" id="addeventag" />
							<a href="javascript:void(0)" id="addmobisoc">+</a>
						</div>
					</div>

					<div class="mobitems">
						<span class="fl mobiname fontsize14">Tracking URL</span>
						<div class="fl mobiurl" style="height: 100px;" id="tracking_url"></div>
						<div class="clear"></div>
					</div>
					<div class="text-r"><button type="submit" style="border: 0;" class="btn-popup colwhite fontsize16 J_ajax_submit_btn">Save & Close</button> <a type="button"  title="are you sure?" href="/index.php?option=com_console&Itemid=127&task=CampaignChannel&action=delete&id=<?php echo $item->id?>" style="border: 0;" class="layer_confirm btn-popup colwhite fontsize16">Delete & Close</a></div>

				</div>
			</div>
		</div>
	</form>
	<script type="text/javascript">
		$(function(){
			var appid = '<?php echo $app_id;?>';
			var appurl = '/index.php?option=com_console&view=campaigntracking&Itemid=127&task=CampaignTracking&action=ajax';
			getEvent(appid,appurl);

			var channel_id = $('#channel_id').val();
			getTracking(channel_id,'/index.php?option=com_console&Itemid=127&task=CampaignChannel&action=ajax');
			$('#channel_id').change(function () {
				var channel_id = $(this).val();
				getTracking(channel_id,'/index.php?option=com_console&Itemid=127&task=CampaignChannel&action=ajax');
			});
		});

		function getEvent(appid,url) {
			if (appid > 0) {
				$("#addsearch").empty();
				var url = url+'&appid='+appid;
				$.getJSON(url, function (data) {
					$.each(data,function(i,v){
						$("#addsearch").append("<option value='"+ v.event_id+"'>"+ v.event_name+"</option>");
					})
				});
			}
		}
		function getTracking(channel_id,url) {
			if (channel_id > 0) {

				var url = url+'&channel_id='+channel_id;
				$.getJSON(url, function (data) {
					var open_id = '<?php echo $campaign->open_id?>';
					var tracking_url = data.tracking_url;
					tracking_url = tracking_url.replace('{openId}',open_id);
					tracking_url = tracking_url.replace('{partnerId}',data.id);
					$('#tracking_url').html(tracking_url);
				});
			}
		}
	</script>
<?php else: //list?>

<?php endif; //endlist?>
<script type="text/javascript" src="/templates/ppspconsole/js/select2.min.js"></script>
<script type="text/javascript" src="/templates/ppspconsole/js/jquery.range.js"></script>
<script type="text/javascript" src="/templates/ppspconsole/js/zh-CN.js"></script>
<script>
$(function(){

    $("select").select2();
	$(".eventile span").click(function(){
		if($(this).attr("class")=="selected"){
			$(this).removeClass();
            $(".enable-content").fadeOut();
            $(this).parent().find("input[name='enable_sync']").val("0");
            $(".eventSync-nr").fadeOut();
		}else{
			$(this).addClass("selected");
            $(".enable-content").fadeIn();
            $(this).parent().find("input[name='enable_sync']").val("1");
            $(".eventSync-nr").fadeIn();
		}
	});
    $("#cost").click(function(){
        if($(this).attr("class")=="selected"){
            $(this).removeClass();
            $(this).parent().find("input[name='cost']").val("0");
            $(this).parent().find("input[name='cost_src']").val("0");
            $(this).parent().find("input[name='cost']").removeAttr("disabled");
        }else{
            $(this).addClass("selected");
            $(this).parent().find("input[name='cost']").val("-1");
            $(this).parent().find("input[name='cost_src']").val("1");
            $(this).parent().find("input[name='cost']").prop("disabled","true");
        }
    })
	var MaxInputs       = 6; //最多可以添加几行
	var InputsWrapper   = $("#InputsWrapper"); //添加到哪里 
	var AddButton       = $("#addmobisoc"); //点击添加按钮id
	var x = InputsWrapper.length; 
	var FieldCount=1; 

	$(AddButton).click(function(e){ 
        var addsearch = $("#addsearch").find("option:selected").text();
        var addsearchval = $("#addsearch").val();
        var addeventag = $("#addeventag").val(); 
		if(x <= MaxInputs)
		{
			FieldCount++;
			$(InputsWrapper).append('<tr><td>'+addsearch+'<input type="hidden" name="map[event_id][]" value='+addsearchval+' /></td><td>'+addeventag+'<input type="hidden" name="map[event_tag][]" value='+addeventag+' /></td><td><a href="javascript:void(0)" class="removetflist glyphicon glyphicon-remove colred"></a></td></tr>');
			x++;
		}
		//return false;  
	});
  
	$("body").on("click",".removetflist", function(e){
		FieldCount--;
		$(this).parent().parent('tr').remove(); //删除当前点击的div
		x--;
		return false;  
	});

	$(".f-selct-content p").click(function(){
		$("#selectext").val($(this).text());
	});
	$(".f-selct-content-ad p").click(function(){
		$("#networktxt").val($(this).text());
	});
})

</script>